package com.kgc.springboot.aviation.service.zzy.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.springboot.aviation.mapper.CollectionMapper;
import com.kgc.springboot.aviation.mapper.DirectoryMapper;
import com.kgc.springboot.aviation.mapper.UserMapper;
import com.kgc.springboot.aviation.pojo.*;
import com.kgc.springboot.aviation.service.zzy.AllService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("AllService")
public class AllServiceImpl implements AllService {


    @Resource
    DirectoryMapper directoryMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    CollectionMapper collectionMapper;

    @Override
    public PageInfo<Directory> selectAll(Integer pageNum, Integer pageSize, Integer id) {
        PageHelper.startPage(pageNum, pageSize);
        DirectoryExample directoryExample = new DirectoryExample();
        List<Directory> directories = null;
        if (id != 0) {
            directoryExample.createCriteria().andLawsIdEqualTo(id);
            directories = directoryMapper.selectByExample(directoryExample);
        } else {
            directories = directoryMapper.selectByExample(null);

        }
        PageInfo<Directory> pageInfo = new PageInfo<>(directories);
        return pageInfo;

    }

    @Override
    public List<Directory> getSelect(Integer lawsId) {
        DirectoryExample example = new DirectoryExample();
        if (lawsId != null && lawsId > 0) {
            example.createCriteria().andLawsIdEqualTo(lawsId);
            List<Directory> directories = directoryMapper.selectByExample(example);
            return directories;
        }

        return null;
    }

    @Override
    public int add(Directory directory) {
        return directoryMapper.insertSelective(directory);
    }

    @Override
    public PageInfo<User> selectAllUser(Integer pageNum, Integer pageSize, String userUname, String userPhone) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("id");
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        if (userUname != null && !userUname.equals("")) {
            System.out.println("进来名字判断");
            criteria.andUserUnameLike("%" + userUname + "%");
        }
        if (userPhone != null && !userPhone.equals("")) {
            System.out.println("进来手机判断");
            criteria.andUserPhoneLike("%" + userPhone + "%");
        }
        List<User> users = userMapper.selectByExample(userExample);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public int addCollection(Collection collection) {

        int i = collectionMapper.insertSelective(collection);

        return i;
    }

    @Override
    public int deleteConllection(Integer userId, Integer clauseId) {
        CollectionExample collectionExample = new CollectionExample();
        collectionExample.createCriteria().andUserIdEqualTo(userId).andClauseIdEqualTo(clauseId);
        int i = collectionMapper.deleteByExample(collectionExample);
        return i;
    }

    @Override
    public int delConllection(Integer userId, Integer id) {
        CollectionExample collectionExample = new CollectionExample();
        collectionExample.createCriteria().andUserIdEqualTo(userId).andIdEqualTo(id);
        int i = collectionMapper.deleteByExample(collectionExample);
        return i;
    }

    @Override
    public PageInfo<Collection> selectCollection(Integer pageNum, Integer pageSize, Integer userId) {
        PageHelper.startPage(pageNum, pageSize);
        CollectionExample collectionExample = new CollectionExample();
        collectionExample.createCriteria().andUserIdEqualTo(userId);

        List<Collection> collections = collectionMapper.selectByExample(collectionExample);

        PageInfo<Collection> pageInfo = new PageInfo<>(collections);
        return pageInfo;
    }

    @Override
    public Directory getId(Integer lawsId) {
        DirectoryExample example= new DirectoryExample();
        if (lawsId!=null&&lawsId>0){
            example.createCriteria().andLawsIdEqualTo(lawsId);
            List<Directory> directories = directoryMapper.selectByExample(example);
            if (directories!=null&&directories.size()>0){
                return directories.get(0);
            }

        }
        return null;
    }

    @Override
    public Directory upGetAll(Integer id) {
        DirectoryExample example=new DirectoryExample();
        if (id!=null&&id>0){
            example.createCriteria().andIdEqualTo(id);
            List<Directory> directories = directoryMapper.selectByExample(example);
            if (directories!=null&&directories.size()>0){
                return  directories.get(0);
            }
        }
        return null;
    }

    @Override
    public int upDate(Directory directory) {
        return directoryMapper.updateByPrimaryKeySelective(directory);
    }


}
